<?php 
class Automobile_model extends CI_Model {

	/**
	 * @author Pwint Phyu Shwe
	 * Property Model for tbl_property, tbl_address and tbl_company tables
	 * CRUD of Automobile Related.
	 * This model include pagination and related functions.
	 * Refer detail in each function's comment section below.
	 * This model is related to automobile advertisements.
	 */
	// table name
	private $tbl_automobile = 'automobile';
	private $tbl_type = 'automobile_type';
	private $tbl_user = 'masteruser';
	private $tbl_brand = 'automobile_brand';

	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
	}

	// get number of users in database
	function count_all(){
		return $this->db->count_all($this->tbl_automobile);
	}
	//get totoal number of result in database
	function count_result($cond = NULL){
		//filter
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($param , $item);
		}
		$this->db->where('expired_date > NOW()');

		$this->db->select('COUNT(*) as count', FALSE);
		$this->db->from($this->tbl_automobile);
		$this->db->join($this->tbl_user, 'masteruser.userID = automobile.advertiserID', 'left');
		$this->db->join($this->tbl_type, 'automobile_type.id = automobile.typeID', 'left');
		$this->db->join($this->tbl_brand,'automobile_brand.brand_id = automobile.brandID', 'left');

		$query = $this->db->get();
		$results = $query->result();
		$num_rows = $results[0]->count;
		$query->free_result();
		return $num_rows;
		
	}
	// get result based on conditions
	function get_result($cond = NULL, $limit = 10, $offset = 0, $sort = NULL){
		//filter
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($param , $item);
		}
		$this->db->where('expired_date > NOW()');
		//sorting
		if ($sort != NULL){
			for ($i = 0; $i < count($sort); $i++)
				$this->db->order_by($sort[$i]['column'], $sort[$i]['order']);
		}
		//limit & offset
		$this->db->limit($limit, $offset);

		$this->db->select('*', 'automobile_type.auto_type', 'automobile_brand.brand_name','masteruser.firstName', 'masteruser.lastName');
		$this->db->from($this->tbl_automobile);
		$this->db->join($this->tbl_user, 'masteruser.userID = automobile.advertiserID', 'left');
		$this->db->join($this->tbl_type, 'automobile_type.id = automobile.typeID', 'left');
		$this->db->join($this->tbl_brand,'automobile_brand.brand_id = automobile.brandID', 'left');

		$query = $this->db->get();
		$results['rows'] = $query->result();
		$results['num_rows'] = $this->count_result($cond);
		$query->free_result(); //free result
		return $results;
	}
	// get by price range, cond will have 'low' and 'high' price range as array
	function get_by_pricerange($price_range = NULL, $cond=NULL, $limit = 10, $offset = 0, $sort = NULL){
	
		if ($price_range == NULL)
			return false;
		//condition
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($param , $item);
		}
		//filter expiry date
		$this->db->where('expired_date > NOW()');
		//sorting
		if ($sort != NULL){
			for ($i = 0; $i < count($sort); $i++)
				$this->db->order_by($sort[$i]['column'], $sort[$i]['order']);
		}
		
		//limit & offset
		$this->db->limit($limit, $offset);
		
		$this->db->select('*', 'automobile_type.auto_type', 'automobile_brand.brand_name','masteruser.firstName', 'masteruser.lastName');
		$this->db->from($this->tbl_automobile);
		$this->db->join($this->tbl_user, 'masteruser.userID = automobile.advertiserID', 'left');
		$this->db->join($this->tbl_type, 'automobile_type.id = automobile.typeID', 'left');
		$this->db->join($this->tbl_brand,'automobile_brand.brand_id = automobile.brandID', 'left');
		//result
		$query = $this->db->get();
		$result = $query->result_array();
		$query->free_result(); //free result
		return $result;
	}
	// get search result
	function get_search_result($search = NULL, $cond = NULL, $limit = 10, $offset = 0, $sort = NULL)
	{
		//filter
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($param , $item);
		}
		//filter expiry date
		$this->db->where('expired_date > NOW()');
		//sorting
		if ($sort != NULL){
			for ($i = 0; $i < count($sort); $i++)
				$this->db->order_by($sort[$i]['column'], $sort[$i]['order']);
		}
		//limit & offset
		$this->db->limit($limit, $offset);
		//query
		$this->db->select('*', 'automobile_type.auto_type', 'automobile_brand.brand_name','masteruser.firstName', 'masteruser.lastName');
		$this->db->from($this->tbl_automobile);
		if ($search)
			$this->db->where("(`title` LIKE  '%" . $search ."%' OR  `description` LIKE  '%". $search ."%')");
		
		$this->db->join($this->tbl_user, 'masteruser.userID = automobile.advertiserID', 'left');
		$this->db->join($this->tbl_type, 'automobile_type.id = automobile.typeID', 'left');
		$this->db->join($this->tbl_brand,'automobile_brand.brand_id = automobile.brandID', 'left');
		$query = $this->db->get();
		log_message('INFO', "Automobile Query--->" . $search . $this->db->last_query());
		
		$results['rows'] = $query->result();
		$results['num_rows'] = $this->count_search_result($search, $cond);
		$query->free_result(); //free result
		return $results;
	
	}
	/**
	get totoal number of search result in database
	return $count
	**/
	function count_search_result($search = NULL, $cond = NULL){
		//filter
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($param , $item);
		}
		//filter expiry date
		$this->db->where('expired_date > NOW()');
		$this->db->select('COUNT(*) as count');
		$this->db->from($this->tbl_automobile);
		if ($search)
			$this->db->where("(`title` LIKE  '%" . $search ."%' OR  `description` LIKE  '%". $search ."%')");
		
		$this->db->join($this->tbl_user, 'masteruser.userID = automobile.advertiserID', 'left');
		$this->db->join($this->tbl_type, 'automobile_type.id = automobile.typeID', 'left');
		$this->db->join($this->tbl_brand,'automobile_brand.brand_id = automobile.brandID', 'left');
		
		$query = $this->db->get();
		log_message('INFO', "Automobile Count Query--->" . $this->db->last_query());
		$results = $query->result();
		$num_rows = $results[0]->count;
		$query->free_result();
		return $num_rows;
	}
	// add new advertisement
	function save($automobile){
		//current & expiry date
		$current_date = date('Y-m-d H:m:s');
		$automobile['posted_date'] = $current_date;
		$this->load->helper('common_functions');
		$automobile['expired_date'] = calculate_expiry_date($current_date, '+60 day');
		//save property object
		$this->db->insert($this->tbl_automobile, $automobile);
		return $this->db->insert_id();
	}
	// update person by userID
	function update($updated_auto){
		//property informations
		$auto['automobileID'] = $updated_auto['automobileID'];
		$auto['advertiserID'] = $updated_auto['advertiserID'];
		$auto['categoryID'] = $updated_auto['categoryID'];
		$auto['typeID'] = $updated_auto['typeID'];
		$auto['brandID'] = $updated_auto['brandID'];

		$auto['title'] = $updated_auto['title'];
		$auto['description'] = $updated_auto['description'];
		$auto['features'] = $updated_auto['features'];
		$auto['model'] = $updated_auto['model'];
		$auto['price'] = $updated_auto['price'];
		$auto['is_negotiable'] = $updated_auto['is_negotiable'];

		$auto['posted_date'] = $updated_auto['posted_date'];
		$auto['expired_date'] = $updated_auto['expired_date'];
		//contact person information
		$auto['contact_person'] = $updated_auto['contact_person'];
		$auto['contact_number'] = $updated_auto['contact_number'];
		$auto['contact_email'] = $updated_auto['contact_email'];
		
		//update
		$this->db->where('automobileID',$updated_auto['automobileID']);
		$this->db->update($this->tbl_automobile, $auto);
	}
	// delete person by advertiserID
	function delete($id){
		$this->db->where('automobileID', $id);
		$this->db->delete($this->tbl_automobile);
	}
}
/* End of file automobile_model.php */
/* Location: ./application/models/automobile_model.php */

?>